<?php
namespace app\api\controller;
use app\common\bean\ListMap;
use app\common\bean\SystemRechargeBean;
use app\common\bean\UserBean;
use app\common\bean\UserRechargeBean;
use app\common\controller\ApiController;
use app\common\enum\UserRechargeStatusEnum;
use app\common\manage\UserManage;
use app\common\manage\UserRechargeManage;
use app\common\model\SystemRechargeModel;
use app\common\model\UserRechargeModel;
use app\common\utils\CommonUtil;
use app\common\utils\ExitJsonUtil;

class Recharge extends ApiController
{
    protected $beforeActionList = [
        'decryptToken' => ['except' => 'callBack'],
    ];
    public function index(){//充值记录列表
        $listMap = new ListMap();
    }
    public function callBack(){
        $cbData = CommonUtil::getInstance()->backData();
        //file_put_contents('callback.txt', date('Y-m-d H:i:s')."-{$cbData['out_trade_no']}\n", FILE_APPEND);
        $recharge = UserRechargeModel::getInstance()->selectData([UserRechargeBean::$orderNumber=>$cbData['out_trade_no']]);
        if($recharge[UserRechargeBean::$status]==UserRechargeStatusEnum::WAITPAY){
            $userBean = new UserBean();
            $userBean->setId($recharge[UserRechargeBean::$userId]);
            $user = UserManage::getInstance()->getInfo($userBean);
            $recharge['blance'] = $user->getBlance();
            UserRechargeManage::getInstance()->callBack($recharge);
            ExitJsonUtil::getInstance()->exitData([], 200, 'success');
        }else{
            ExitJsonUtil::getInstance()->exitData($recharge, 501, '已经充值');
        }
    }
    public function account(){//计算到账余额
        $give_money = UserRechargeManage::getInstance()->getAccount( $this->postParams['money'] );
        $data['give_money'] = $give_money;
        $data['account'] = $give_money+$this->postParams['money'];
        ExitJsonUtil::getInstance()->exitData($data);
    }
    public function add(){//创建充值订单
        $bean = new UserRechargeBean();
        $bean->setData($this->postParams);
        $bean->setUserId($this->userId);
        $bean = UserRechargeManage::getInstance()->add( $bean );

        $bean->setParameter('openid', $this->openId);
        $res = UserRechargeManage::getInstance()->pay($bean);
        ExitJsonUtil::getInstance()->exitData( $res );
    }
    public function systemRecharge(){//获取充值列
        $list = SystemRechargeModel::getInstance()->selectData([], true, SystemRechargeBean::$quota.' ASC');
        ExitJsonUtil::getInstance()->exitData( $list );
    }
}